package com.gkzf.ai.module.crm.util.assembly;

import com.alibaba.fastjson.JSONObject;
import com.gkzf.ai.module.crm.controller.admin.creditReport.vo.creditreportdetails.CreditReportDetailsSaveReqVO;

import java.util.Map;

import static com.gkzf.ai.module.crm.util.packaging.CalculationAccountSummary.ratio;

public class LoanSummary {

    private static final String  CREDIT_TRANSACTION_INFO_SUMMARY = "信贷交易授信及负债信息概要（未结清/未销户）";

    private static final String  CREDIT_CARD_ACCOUNT_SUMMARY = "贷记卡账户信息汇总";

    private static final String  QUASI_CREDIT_CARD_ACCOUNT_SUMMARY = "准贷记卡账户信息汇总";

    private static final String  REVOLVING_LOAN_ONE_ACCOUNT_SUMMARY = "循环贷账户一信息汇总";

    private static final String  REVOLVING_LOAN_TOW_ACCOUNT_SUMMARY = "循环贷账户二信息汇总";

    private static final String  NON_REVOLVING_LOAN_ACCOUNT_SUMMARY = "非循环贷账户信息汇总";

    public static final String ACCOUNT_NUMBER = "账户数";

    public static final String TOTAL_CREDIT_LINE = "授信总额";

    public static final String BALANCE = "余额";

    public static final String AVERAGE_SERVICE_REPAYMENT = "最近6个月平均应还款";

    public static final String USED_CREDIT = "已用额度";

    public static final String CARD_ISSUER_NUMBER = "发卡机构数";

    public static final String AVERAGE_SERVICE_RATE = "最近6个月平均使用额度";


    public void getLoanSummary(Map<String, Object> param, CreditReportDetailsSaveReqVO creditReportDetailsSaveReqVO){
        if (param.containsKey(CREDIT_TRANSACTION_INFO_SUMMARY)){
            JSONObject creditTransactionInfoSummary = (JSONObject)param.get(CREDIT_TRANSACTION_INFO_SUMMARY);
            if (null != creditTransactionInfoSummary && creditTransactionInfoSummary.containsKey(NON_REVOLVING_LOAN_ACCOUNT_SUMMARY)){
                JSONObject nonRevolvingLoanAccountSummary = (JSONObject)creditTransactionInfoSummary.get(NON_REVOLVING_LOAN_ACCOUNT_SUMMARY);
                creditReportDetailsSaveReqVO.setDebtNonCyclicalAccountCount(AssembleUtils.getStringInteger(nonRevolvingLoanAccountSummary, ACCOUNT_NUMBER));
                creditReportDetailsSaveReqVO.setDebtNonCyclicalGrossAmount(AssembleUtils.getBigDecimalValue(nonRevolvingLoanAccountSummary, TOTAL_CREDIT_LINE));
                creditReportDetailsSaveReqVO.setDebtNonCyclicalBalance(AssembleUtils.getBigDecimalValue(nonRevolvingLoanAccountSummary, BALANCE));
                creditReportDetailsSaveReqVO.setDebtNonCyclicalMonthAverage(AssembleUtils.getBigDecimalValue(nonRevolvingLoanAccountSummary, AVERAGE_SERVICE_REPAYMENT));
            }
            if (null != creditTransactionInfoSummary && creditTransactionInfoSummary.containsKey(REVOLVING_LOAN_ONE_ACCOUNT_SUMMARY)){
                JSONObject revolvingLoanOneAccountSummary = (JSONObject)creditTransactionInfoSummary.get(REVOLVING_LOAN_ONE_ACCOUNT_SUMMARY);
                creditReportDetailsSaveReqVO.setDebtOneCyclicalAccountCount(AssembleUtils.getStringInteger(revolvingLoanOneAccountSummary, ACCOUNT_NUMBER));
                creditReportDetailsSaveReqVO.setDebtOneCyclicalGrossAmount(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, TOTAL_CREDIT_LINE));
                creditReportDetailsSaveReqVO.setDebtOneCyclicalBalance(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, BALANCE));
                creditReportDetailsSaveReqVO.setDebtOneCyclicalMonthAverage(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, AVERAGE_SERVICE_REPAYMENT));
            }
            if (null != creditTransactionInfoSummary && creditTransactionInfoSummary.containsKey(REVOLVING_LOAN_TOW_ACCOUNT_SUMMARY)){
                JSONObject revolvingLoanOneAccountSummary = (JSONObject)creditTransactionInfoSummary.get(REVOLVING_LOAN_TOW_ACCOUNT_SUMMARY);
                creditReportDetailsSaveReqVO.setDebtTowCyclicalAccountCount(AssembleUtils.getStringInteger(revolvingLoanOneAccountSummary, ACCOUNT_NUMBER));
                creditReportDetailsSaveReqVO.setDebtTowCyclicalGrossAmount(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, TOTAL_CREDIT_LINE));
                creditReportDetailsSaveReqVO.setDebtTowCyclicalBalance(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, BALANCE));
                creditReportDetailsSaveReqVO.setDebtTowCyclicalMonthAverage(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, AVERAGE_SERVICE_REPAYMENT));
            }
            if (null != creditTransactionInfoSummary && creditTransactionInfoSummary.containsKey(CREDIT_CARD_ACCOUNT_SUMMARY)){
                JSONObject revolvingLoanOneAccountSummary = (JSONObject)creditTransactionInfoSummary.get(CREDIT_CARD_ACCOUNT_SUMMARY);
                creditReportDetailsSaveReqVO.setCreditCardOrgNumber(AssembleUtils.getStringInteger(revolvingLoanOneAccountSummary, CARD_ISSUER_NUMBER));
                creditReportDetailsSaveReqVO.setCreditCardAccountNumber(AssembleUtils.getStringInteger(revolvingLoanOneAccountSummary, ACCOUNT_NUMBER));
                creditReportDetailsSaveReqVO.setCreditCardMaxMoney(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, TOTAL_CREDIT_LINE));
                creditReportDetailsSaveReqVO.setCreditCardUsedCredit(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, USED_CREDIT));
                creditReportDetailsSaveReqVO.setCreditCardMonthAverage(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, AVERAGE_SERVICE_RATE));
                creditReportDetailsSaveReqVO.setCreditCardCurrentRate(ratio().apply(creditReportDetailsSaveReqVO.getCreditCardUsedCredit(), creditReportDetailsSaveReqVO.getCreditCardMaxMoney()));
                creditReportDetailsSaveReqVO.setCreditCardMonthRete(ratio().apply(creditReportDetailsSaveReqVO.getCreditCardMonthAverage(), creditReportDetailsSaveReqVO.getCreditCardMaxMoney()));
            }
//            if (null != creditTransactionInfoSummary && creditTransactionInfoSummary.containsKey(QUASI_CREDIT_CARD_ACCOUNT_SUMMARY)){
//                JSONObject revolvingLoanOneAccountSummary = (JSONObject)creditTransactionInfoSummary.get(QUASI_CREDIT_CARD_ACCOUNT_SUMMARY);
//                creditReportDetailsSaveReqVO.setCreditCardOrgNumber(AssembleUtils.getStringInteger(revolvingLoanOneAccountSummary, CARD_ISSUER_NUMBER));
//                creditReportDetailsSaveReqVO.setCreditCardAccountNumber(AssembleUtils.getStringInteger(revolvingLoanOneAccountSummary, ACCOUNT_NUMBER));
//                creditReportDetailsSaveReqVO.setCreditCardMaxMoney(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, TOTAL_CREDIT_LINE));
//                creditReportDetailsSaveReqVO.setCreditCardUsedCredit(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, USED_CREDIT));
//                creditReportDetailsSaveReqVO.setCreditCardMonthAverage(AssembleUtils.getBigDecimalValue(revolvingLoanOneAccountSummary, AVERAGE_SERVICE_RATE));
//            }
        }
    }


}
